﻿// See https://aka.ms/new-console-template for more information
using System.IO.Pipes;
using System.Text;

Console.WriteLine("Named Pipe Server is running...适合本地机器高效通讯");
using (var server = new NamedPipeServerStream("testpipe", PipeDirection.InOut))
{
    Console.WriteLine("等待客户端接入");
    await server.WaitForConnectionAsync();
    Console.WriteLine("客户端已接入");

    //读取客户端消息
    byte[] buffer = new byte[1024];
    int bytesRead = await server.ReadAsync(buffer, 0, buffer.Length);
    var message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
    Console.WriteLine("收到客户端消息" + message);

    //发送响应
    string response = "Hello from server";
    byte[] responseBuffer = Encoding.UTF8.GetBytes(response);
    await server.WriteAsync(responseBuffer, 0, responseBuffer.Length);
    Console.ReadLine();
}
